<?php


namespace app\api\controller;


use think\Controller;
use think\Db;
use think\Exception;
use think\Session;
use think\Validate;

class Admin extends Controller
{

    //获取资讯栏目
    public function classifyApi(){
        try{
            $common = new Common();



            $config = Session::get("thisWechat");
            $where['status'] = 1;
            $where['uniacid'] = $config['platform_id'];
            $where['pid'] = empty($_POST['pid'])?0:$_POST['pid'];

            $data = $common->public_select("admin_news_classify",$where,"id,name")->order("sort desc")->select();

            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取资讯列表
    public function newlistApi(){
        try{
            $common = new Common();

            $config = Session::get("thisWechat");
            $where['status'] = 1;
            $where['uniacid'] = $config['platform_id'];
            $where['classify_id'] = $_POST['classify_id'];

            $page = empty($_POST['page'])?1:$_POST['page'];
            $limit = empty($_POST['limit'])?10:$_POST['limit'];
            $start = ($page-1)*$limit;

            $list = $common->public_select("admin_news",$where,"id,title,thumb,desc,addtime,hits",$start,$limit)->order("sort desc")->select();
            $count = $common->public_select("admin_news",$where)->count("*");
            $black['code'] = 0;
            $black['list'] = $list;
            $black['count'] = $count;
            $black['pageCount'] = ceil($count/$limit);
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //获取资讯详情
    public function newDetailApi(){
        try{
            $common = new Common();

            if(!empty($_POST['id'])){
                $where['id'] = $_POST['id'];
            }else{
                $where['classify_id'] = $_POST['classify_id'];
            }
            $detail = $common->public_select("admin_news",$where)->order("id desc")->find();
            $black['code'] = 0;
            $black['detail'] = $detail;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //登录操作
    public function doLogin(){
        try{
            $rule = [
                ["mobile", "require", "请输入手机号"],
                ["password", "require", "请输入密码"],
            ];
            $validate = new Validate($rule);
            $request = $this->request->post();
            
            
            if (!$validate->check($request)) {
                throw new Exception($validate->getError());
            }

            //$thisWechat = Session::get("thisWechat");
            $appid = $request['appid'];
            $config = Db::name("config")->where(['xcx_appid'=>$appid])->find();
            unset($request['appid']);
            $request['uniacid'] = $config['id'];

            $request['password'] = md5($request['password']);
            $detail = Db::name("admin_fans")->where($request)->find();
            if(empty($detail)){
                throw new Exception("账号不存在或密码错误");
            }

            $userid = $detail['id'];
            Db::name("admin_fans")->where($request)->setInc("number",1);
            Session::set('USER_INFO_SESSION',$detail);
            Session::set("userid",$userid);
            setcookie('userid',$userid,time()+86400*30);
            $black['code'] = 0;
            $black['userid'] = $userid;
            $black['userinfo'] = $detail;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    //退出登录
    public function outlogin(){
        try{
            Session::delete('USER_INFO_SESSION');
            Session::delete('userid');
            $black['code'] = 0;
            $black['msg'] = "退出成功";
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }


    /**
     * 获取用户基本信息
     * @return false|string
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function userinfo(){
        $common = new Common();
        $userid = $common->common();
        try{
            $data = Db::name("admin_fans")->where(['id'=>$userid])->find();
            $black['code'] = 0;
            $black['userinfo'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取公告列表
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function notice(){
        $common = new Common();
        try{
            $appid = $_POST['appid'];
            $config = Db::name("config")->where(['xcx_appid'=>$appid])->find();
            $table = "admin_affiche";
            $where['uniacid'] = $config['id'];
            $where['status'] = 1;
            $field = "id,title";
            $page = empty($_POST['page']) ? 1 : $_POST['page'];
            $limit = empty($_POST['limit']) ? 10 : $_POST['limit'];
            $start = ($page - 1) * $limit;
            $list = $common->public_select($table,$where,$field,$start,$limit)->select();
            $count = $common->public_select($table,$where)->count("*");
            $black['code'] = 0;
            $black['list'] = $list;
            $black['count'] = $count;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取公告列表
     * @return false|string
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function noticeDetail(){
        $common = new Common();
        try{
            $table = "admin_affiche";
            $where['status'] = 1;
            $where['id'] = $_POST['id'];
            $field = "id,title,content";
            $data = $common->public_select($table,$where,$field)->find();
            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 平台标识授权
     * @return false|string
     * @author zhixuedong
     * @date 2021/1/22/022
     */
    public function platformAuto(){
        try{
            $appid = $_POST['appid'];
            $config = Db::name("config")->where(['xcx_appid'=>$appid])->find();
            Session::set("thisWechat",$config);

            $face_type = Db::name("tickets_config")->value("face_type");
            $face_type = empty($face_type)?0:$face_type;

            $black['code'] = 0;
            $black['msg'] = "操作成功";
            $black['config'] = ['faceid'=>$face_type,'bindmobile'=>1];
        }catch (Exception $e){
            $black['code'] = 1;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    /**
     * 获取一键通列表
     * @return false|string
     * @author zhixuedong
     * @date 2021/1/23/023
     */
    public function listApi(){
        try {
            $common = new Common();
            $config =  $common->showappid($_POST['appid']);

            $limit = empty($_POST['limit'])?10:intval($_POST['limit']);
            $page = empty($_POST['page'])?1:intval($_POST['page']);
            $start = ($page-1)*$limit;
            $where['uniacid'] = $config['id'];
            $where['status'] = 1;
            $count = Db::name("admin_content")->where($where)->count("*");
            $list = Db::name("admin_content")->where($where)->limit($start,$limit)->field("id,title,thumb,desc,link,appid,applink")->order("sort desc,id desc")->select();
            $black['code'] = 0;
            $black['msg'] = "获取成功";
            $black['list'] = $list;
            $black['count'] = $count;
            $black['page'] = $page;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = "获取失败";
        }
        return json_encode($black);
    }

    //获取自定义导航
    public function navApi(){
        try{
            $appid = $_POST['appid'];
            $uniacid = Db::name("config")->where(['xcx_appid'=>$appid])->value("id");
            $where['uniacid'] = $uniacid;
            $where['status'] = 1;
            $lists = Db::name("admin_navigation")->where($where)->field("text,pagePath,iconPath,selectedIconPath")->select();
            foreach ($lists as $k=>$v){
                $lists[$k]['iconPath'] = empty($v['iconPath'])?'':"https://" . $_SERVER['HTTP_HOST'] .$v['iconPath'];
                $lists[$k]['selectedIconPath'] = empty($v['selectedIconPath'])?'':"https://" . $_SERVER['HTTP_HOST'] .$v['selectedIconPath'];
            }
            $black['code'] = 0;
            $black['data'] = $lists;
        }catch (Exception $e){
            $black['code'] = 1;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取平台公告
    public function afficheApi(){
        try{
            $common = new Common();
            $config = $common->showappid($_POST['appid']);
            $where['status'] = 1;
            $where['uniacid'] =$config['id'];
            $data = Db::name("admin_affiche")->where($where)->order("sort desc")->field("id,title,Wdate,path")->limit(6)->select();

            foreach ($data as $k=>$v){
                $data[$k]['path'] = empty($v['path'])?'':"https://" . $_SERVER['HTTP_HOST'] .$v['path'];
            }

            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //拉取栏目信息
    public function columnApi(){
        try{
            $common = new Common();
            $config = $common->showappid($_POST['appid']);
            $where['status'] = 1;
            $where['uniacid'] = $config['id'];
            $data = Db::name("admin_column")->where($where)->order("sort desc")->field("id,name,logo,link")->limit(4)->select();
            //拼接图片
            $preg = "/^http(s)?:\\/\\/.+/";
            foreach ($data as $k=>$v){
                if(empty($v['logo'])){
                    $data[$k]['logo'] = "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" ;
                }else{
                    if(!preg_match($preg,$v['logo']))
                    {
                        $data[$k]['logo'] = "https://" . $_SERVER['HTTP_HOST'] . $v['logo'];
                    }
                }
                //$data[$k]['logo'] = empty($v['logo']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" : "https://" . $_SERVER['HTTP_HOST'] . $v['logo'];
            }
            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取平台广告
    public function bannerApi(){
        try{

            $common = new Common();
            $config = $common->showappid($_POST['appid']);
            $where['status'] = 1;
            $where['uniacid'] = $config['id'];
            $data = Db::name("admin_banner")->where($where)->order("sort desc")->field("id,name,path,link")->limit(5)->select();
            //拼接图片
            $preg = "/^http(s)?:\\/\\/.+/";
            foreach ($data as $k=>$v){
                if(empty($v['path'])){
                    $data[$k]['path'] = "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png" ;
                }else{
                    if(!preg_match($preg,$v['path']))
                    {
                        $data[$k]['path'] = "https://" . $_SERVER['HTTP_HOST'] . $v['path'];
                    }
                }
                //$data[$k]['path'] = empty($v['path']) ? "https://" . $_SERVER['HTTP_HOST'] . "/static/images/nopic-107.png":"https://" . $_SERVER['HTTP_HOST'] . $v['path'];
            }
            $black['code'] = 0;
            $black['data'] = $data;
        }catch (Exception $e){
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

    //获取平台动画
    public function cartoonApi(){
        try{
            $appid = $_POST['appid'];
            $uniacid = Db::name("config")->where(['xcx_appid'=>$appid])->value("id");
            $where['uniacid'] = $uniacid;
            $where['status'] = 1;
            $detail =  Db::name("admin_cartoon")->where($where)->order("sort desc,id desc")->field("id,title,thumb,writing,times,voice,x_view,y_view")->find();
            $black['code'] = 0;
            $black['data'] = $detail;
        }catch (Exception $e){
            $black['code'] = 1;
            $black['msg'] = $e->getMessage();
        }
        return json_encode($black);
    }

}